<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/13 15:48
 */

namespace App\Http\Models;

class VoucherLog extends BaseModel
{
    protected $table = 'users_voucher_log';
    protected $guarded = [];

    /**
     * 对应用户表
     */
    public function user()
    {
        return $this->hasOne('App\Http\Models\User', 'id', 'userid');
    }

    /**
     * @param $userId  用户id
     * @param $content 点券消费内容
     * @param $number 点券充值/消费数量
     * @param $status 1商品退回 ,0 发布商品消费 ，2充值
     * @param $goods_id 商品id
     * @param $refuse 拒绝原因
     * @return mixed
     */
    public static function addUserVoucherLog($userId, $content, $number, $status = 0, $goods_id = 0, $refuse = '')
    {
        $UserCapital = UserCapital::where(['user_id' => $userId])->first();
        $data = [
            'user_id' => $userId,
            'content' => $content,
            'number' => $number,
            'status' => $status,
            'goods_id' => $goods_id,
            'refuse' => $refuse,
            'balance' => ($UserCapital) ? $UserCapital->voucher_number : 0,
        ];
        $log_bool = self::create($data);
        return $log_bool;
    }

}